package com.org.forex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.jfree.date.DateUtilities;

public class DataUtils {

	public static Price[] loadDumpFile(String filename) throws IOException,
			ClassNotFoundException {
		FileInputStream fis = new FileInputStream(filename);
		@SuppressWarnings("resource")
		ObjectInputStream iis = new ObjectInputStream(fis);
		return (Price[]) iis.readObject();
	}

	public static void writeDumpFile(Price[] prices, String filename)
			throws IOException {
		FileOutputStream fos = new FileOutputStream(filename);
		@SuppressWarnings("resource")
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(prices);
	}

	public static Price[] loadTextFile(String filename) throws IOException {
		File file = new File(filename);
		List<String> lines = FileUtils.readLines(file);
		Price[] prices = new Price[lines.size()];
		int i = 0;
		for (String str : lines) {
			if (i > 0) {
				String[] ele = str.split(",");
				int date_int = Integer.parseInt(ele[1]);
				int time_int = Integer.parseInt(ele[2]);
				int year = date_int / 10000;
				int month = (date_int - year * 10000) / 100;
				int day = (date_int - year * 10000 - month * 100);
				int hour = time_int / 10000;
				int min = (time_int - hour * 10000) / 100;
				Date date = DateUtilities.createDate(year, month, day, hour,
						min);
				float value_open = Float.parseFloat(ele[3]);
				float value_high = Float.parseFloat(ele[4]);
				float value_low = Float.parseFloat(ele[5]);
				float value_close = Float.parseFloat(ele[6]);
				float volume = Float.parseFloat(ele[7]);
				prices[i - 1] = new Price(value_open, value_high, value_low,
						value_close, date, volume);
			}
			i++;
			if (i % 10000 == 0)
				System.out.println(i);
		}
		return prices;
	}

	public static Price[] loadCSVFile(String filename) throws IOException {
		File file = new File(filename);
		List<String> lines = FileUtils.readLines(file);
		Price[] prices = new Price[lines.size() - 1];
		int i = 0;
		for (String str : lines) {
			if (i > 0) {
				String[] ele = str.split(",");
				int date_int = Integer.parseInt(ele[0].replaceAll("\\.", ""));
				int time_int = Integer.parseInt(ele[1].replaceAll(":", "")
						+ "00");
				int year = date_int / 10000;
				int month = (date_int - year * 10000) / 100;
				int day = (date_int - year * 10000 - month * 100);
				int hour = time_int / 10000;
				int min = (time_int - hour * 10000) / 100;
				Date date = DateUtilities.createDate(year, month, day, hour,
						min);

				float value_open = Float.parseFloat(ele[2]);
				float value_high = Float.parseFloat(ele[3]);
				float value_low = Float.parseFloat(ele[4]);
				float value_close = Float.parseFloat(ele[5]);
				float volume = Float.parseFloat(ele[6]);
				prices[i - 1] = new Price(value_open, value_high, value_low,
						value_close, date, volume);
			}
			i++;
			if (i % 10000 == 0)
				System.out.println(i);
		}
		return prices;
	}

}
